<?php
/**
*
* 功能描述（蛋糕控制器）
*
* @author suoya <jlusuoya@gmail.com>
* @version 1
*/

class CtrlCake extends CtrlBase
{
	public $paramCheck = array(
		"showlist" => array(
			"page" => array("GET", "1", "intval", "positive"),
			'type' => array("GET", "0", "intval", "notnegative"),
			'type2' => array("GET", "0", "intval", "notnegative"),
			'cheap' => array("GET", "0", "intval", "notnegative"),
			'visible' => array("GET", "2", "intval", "notnegative"),
			'key' => array("GET", "", "trim", ""),
			'keyword' => array("GET", "", "trim", ""),
		),
		"remove" => array(
			'id' => array("GET", "must", "intval", "positive"),
		),
		"detail" => array(
			'id' => array("GET", "must", "intval", "positive"),
		),
		"prices" => array(
			'id' => array("GET", "must", "intval", "positive"),
		),
		"tomod" => array(
			'id' => array("GET", "must", "intval", "positive"),
		),
		"delprice" => array(
			'cakeid' => array("GET", "must", "intval", "positive"),
			'id' => array("GET", "must", "intval", "positive"),
		),
		'addmodprice' => array(
			'cakeid' => array("GET", "must", "intval", "positive"),
			'id' => array("POST", "must", "intval", "notnegative"),
			'size' => array("POST", "must", "intval", "positive"),
			'describe' => array("POST", "must", "trim", "notempty"),
			'price' => array("POST", "must", "floatval", "notnegative"),
			'unit' => array("POST", "must", "trim", "notempty"),
			'default' => array("POST", "must", "intval", "bool"),
		),
		"add" => array(
			'no' => array('POST', 'must', 'trim', 'notempty'),
			'type' => array('POST', 'must', 'intval', 'positive'),
			'type2' => array('POST', '0', 'intval', 'notnegative'),
			'name' => array('POST', 'must', 'trim', 'notempty'),
			'introduce' => array('POST', 'must', 'trim', 'notempty'),
			'discount' => array('POST', 'must', 'floatval', 'positive'),
			'mainimg' => array('FILES', 'must', '', ''),

			'taste' => array('POST', 'must', 'trim', ''),
			'suits' => array('POST', 'must', 'trim', ''),
			'sweetness' => array('POST', 'must', 'intval', 'notnegative'),
			'storage' => array('POST', 'must', 'trim', ''),
			'holiday' => array('POST', 'must', 'trim', ''),
			'material' => array('POST', 'must', 'trim', ''),
			'showimg' => array('FILES', 'must', '', ''),
			'attr1' => array('POST', 'must', 'intval', 'notnegative'),
			'attr2' => array('POST', 'must', 'intval', 'notnegative'),
			'attr3' => array('POST', 'must', 'intval', 'notnegative'),
			'attr4' => array('POST', 'must', 'intval', 'notnegative'),
			'attr5' => array('POST', 'must', 'intval', 'notnegative'),
		),
		"mod" => array(
			'id' => array("GET", "must", "intval", "positive"),
			'no' => array('POST', 'must', 'trim', 'notempty'),
			'type' => array('POST', 'must', 'intval', 'positive'),
			'type2' => array('POST', 'must', 'intval', 'notnegative'),
			'visible' => array('POST', 'must', 'intval', 'bool'),
			'name' => array('POST', 'must', 'trim', 'notempty'),
			'introduce' => array('POST', 'must', 'trim', 'notempty'),
			'discount' => array('POST', 'must', 'floatval', 'positive'),
			'mainimg' => array('FILES', 'must', '', ''),

			'taste' => array('POST', 'must', 'trim', ''),
			'suits' => array('POST', 'must', 'trim', ''),
			'sweetness' => array('POST', 'must', 'intval', 'notnegative'),
			'storage' => array('POST', 'must', 'trim', ''),
			'holiday' => array('POST', 'must', 'trim', ''),
			'material' => array('POST', 'must', 'trim', ''),
			'showimg' => array('FILES', 'must', '', ''),
			'attr1' => array('POST', 'must', 'intval', 'notnegative'),
			'attr2' => array('POST', 'must', 'intval', 'notnegative'),
			'attr3' => array('POST', 'must', 'intval', 'notnegative'),
			'attr4' => array('POST', 'must', 'intval', 'notnegative'),
			'attr5' => array('POST', 'must', 'intval', 'notnegative'),
			'keepver' => array('POST', 'must', 'intval', 'bool'),
		),
		"setvisible" => array(
			'id' => array("GET", "must", "intval", "positive"),
			'visible' => array("GET", "must", "intval", "bool"),
		),
		"setdis" => array(
			'id' => array("GET", "must", "intval", "positive"),
			'dis' => array("GET", "must", "floatval", "notnegative"),
		),
	);

	public $perm = array('cake', "index.php?action=login.tologin");

	function ShowList()
	{
		$type = $this->params['type'];
		$type2 = $this->params['type2'];
		$vis = $this->params['visible'];
		$key = $this->params['key'];
		$keyword = $this->params['keyword'];
		$cheap = $this->params['cheap'];

		$obj = $this->GetModel("cake");
		$data = $obj->GetList($this->params['page'], $type, $type2, $vis, $cheap, $key, $keyword);
		$this->assign("listdata", $data['data']);
		$this->assign("totalCnt", $data['cnt']);
		$this->assign("pageCnt", $data['pageCnt']);
		$this->assign("curPage", $this->params['page']);
		$this->assign('type', $type);
		$this->assign('type2', $type2);
		$this->assign('visible', $vis);
		$this->assign('key', $key);
		$this->assign('keyword', $keyword);
		$this->assign('cheap', $cheap);
		return RT_TPL;
	}

	function Detail()
	{
		$obj = $this->GetModel("cake");
		$id = $this->params['id'];
		$data = $obj->GetCake($id);
		if(!$data)
		{
			$this->assign("msg", "蛋糕不存在");
			return RT_MSG;
		}
		$this->assign("data", $data);
		$good = $obj->GetInfo($id);
		if(!$good)
		{
			$this->assign("msg", "蛋糕属性不存在");
			return RT_MSG;
		}
		$this->assign("info", $good);
		return RT_TPL;
	}

	function Prices()
	{
		$obj = $this->GetModel("cake");
		$id = $this->params['id'];
		$data = $obj->GetBaseInfo($id);
		if(!$data)
		{
			$this->assign("msg", "蛋糕不存在");
			return RT_MSG;
		}
		$this->assign("data", $data);
		$data = $obj->GetPrices($id);
		$this->assign("prices", $data);
		return RT_TPL;
	}

	function ToAdd()
	{
		return RT_TPL;
	}

	function ToMod()
	{
		$obj = $this->GetModel("cake");
		$id = $this->params['id'];
		$data = $obj->GetCake($id);
		if(!$data)
		{
			$this->assign("msg", "蛋糕不存在");
			return RT_MSG;
		}
		$this->assign("data", $data);
		$good = $obj->GetInfo($id);
		if(!$good)
		{
			$this->assign("msg", "蛋糕属性不存在");
			return RT_MSG;
		}
		$this->assign("info", $good);
		return RT_TPL;
	}

	function Add()
	{
		$file1 = $this->params['mainimg'];
		$file2 = $this->params['showimg'];
		if(empty($file1['name']) || empty($file2['name']))
		{
			$this->assign("msg", "必须上传两张图片");
			return RT_MSG;
		}

		$obj = $this->GetModel("cake");
		$data1 = array(
			'no' => $this->params['no'],
			'type' => $this->params['type'],
			'type2' => $this->params['type2'],
			'visible' => 0,
			'name' => $this->params['name'],
			'introduce' => $this->params['introduce'],
			'discount' => $this->params['discount'],
		);
		$data2 = array(
			'taste' => $this->params['taste'],
			'suits' => $this->params['suits'],
			'sweetness' => $this->params['sweetness'],
			'storage' => $this->params['storage'],
			'holiday' => $this->params['holiday'],
			'material' => $this->params['material'],
			'attr1' => $this->params['attr1'],
			'attr2' => $this->params['attr2'],
			'attr3' => $this->params['attr3'],
			'attr4' => $this->params['attr4'],
			'attr5' => $this->params['attr5'],
		);
		$id = $obj->NewCake($data1, $data2);
		if($id <= 0)
		{
			$this->assign("msg", "插入失败");
			return RT_MSG;
		}
		$this->CoreLog('cake_addbase', $id, 0, '');
		$msg = "";
		$files = array(
			"cake_${id}.jpg" => $file1,
			"cake_${id}_1.jpg" => $file2,
		);
		foreach($files as $name => $file)
		{
			$msg .= $file['name'];
			$ret = $this->StoreFile($file, $name);
			if($ret != 0)
			{
				switch($ret)
				{
				case 1: $msg .= "文件类型不支持"; break;
				case 2: $msg .= "文件大小超出限制"; break;
				case 3: $msg .= "上传错误"; break;
				case 4: $msg .= "存储错误"; break;
				}
				$this->assign("msg", $msg);
				return RT_MSG;
			}
			$msg .= "上传成功\n";
			$this->CoreLog('cake_addimg', $id, 0, $name);
		}
		$this->assign("msg", "添加成功，id:$id");
		return RT_MSG;
	}

	function SetDis()
	{
		$obj = $this->GetModel("cake");
		$id = $this->params['id'];
		$dis = $this->params['dis'];
		$data = array(
			'discount' => $dis / 10,
		);
		$cnt = $obj->UpdateBase($id, $data);
		if($cnt == 0)
			echo "2";
		else
		{
			echo "0";
			$this->CoreLog('cake_discount', $id, 0, "$dis");
		}
		return RT_NONE;
	}

	function SetVisible()
	{
		$obj = $this->GetModel("cake");
		$id = $this->params['id'];
		$vis = $this->params['visible'];
		$data = array(
			'visible' => $vis,
		);
		$cnt = $obj->UpdateBase($id, $data);
		if($cnt == 0)
			echo "2";
		else
		{
			echo "0";
			$this->CoreLog('cake_visible', $id, $vis, '');
		}
		return RT_NONE;
	}

	function Mod()
	{
		$keepver = $this->params['keepver'];
		$id = $this->params['id'];
		$msg = "";
		$files = array(
			"cake_${id}.jpg" => $this->params['mainimg'],
			"cake_${id}_1.jpg" => $this->params['showimg'],
		);
		$ch = 0;
		foreach($files as $name => $file)
		{
			if(!empty($file['name']))
			{
				$msg .= $file['name'];
				$ret = $this->StoreFile($file, $name);
				if($ret != 0)
				{
					switch($ret)
					{
					case 1: $msg .= "文件类型不支持"; break;
					case 2: $msg .= "文件大小超出限制"; break;
					case 3: $msg .= "上传错误"; break;
					case 4: $msg .= "存储错误"; break;
					}
					$this->assign("msg", $msg);
					return RT_MSG;
				}
				$this->CoreLog('cake_modimg', $id, $keepver, $name);
				$msg .= "上传成功\n";
				$ch++;
			}
		}
		$obj = $this->GetModel("cake");
		$data = array(
			'taste' => $this->params['taste'],
			'suits' => $this->params['suits'],
			'sweetness' => $this->params['sweetness'],
			'storage' => $this->params['storage'],
			'holiday' => $this->params['holiday'],
			'material' => $this->params['material'],
			'attr1' => $this->params['attr1'],
			'attr2' => $this->params['attr2'],
			'attr3' => $this->params['attr3'],
			'attr4' => $this->params['attr4'],
			'attr5' => $this->params['attr5'],
		);
		$cnt = $obj->UpdateInfo($id, $data);
		if($cnt >0 )
		{
			$ch++;
			$this->CoreLog('cake_modbase', $id, $keepver, '');
		}
		$data = array(
			'no' => $this->params['no'],
			'type' => $this->params['type'],
			'type2' => $this->params['type2'],
			'visible' => $this->params['visible'],
			'name' => $this->params['name'],
			'introduce' => $this->params['introduce'],
			'discount' => $this->params['discount'] / 10,
		);
		$cnt = $obj->UpdateBase($id, $data);
		if($cnt > 0)
		{
			$ch++;
			$this->CoreLog('cake_modinfo', $id, $keepver, '');
		}
		if($ch == 0)
			$msg .= "数据无变化或者商品不存在";
		else
		{
			if($keepver == 0)
				$obj->UpdateVersion($id);
			$msg .= "修改成功";
		}
		$this->assign("msg", $msg);
		return RT_MSG;
	}

	function AddModPrice()
	{
		$cakeid = $this->params['cakeid'];
		$id = $this->params['id'];
		$data = array(
			'size' => $this->params['size'],
			'describe' => $this->params['describe'],
			'price' => $this->params['price'],
			'unit' => $this->params['unit'],
			'default' => $this->params['default'],
		);
		$obj = $this->GetModel("cake");
		$ret = "0";
		if($id == 0)
		{
			$data['goods_id'] = $cakeid;
			$id = $obj->AddPrice($data);
			if($id == 0)
				$ret = "2";
			else
			{
				$ret = "0";
				$this->CoreLog('cake_addprice', $cakeid, $id, '');
			}
		}
		else
		{
			$cnt = $obj->ModPrice($id, $data);
			if($cnt == 0)
				$ret = "2";
			else
			{
				$ret = "0";
				$this->CoreLog('cake_modprice', $cakeid, $id, '');
			}
		}
		if($ret == 0)
		{
			$cnt = $this->DefaultPrice($cakeid, $this->params['default'] ? $id : 0);
			if($cnt == 0)
				$ret = 2;
		}
		echo $ret;
		return RT_NONE;
	}

	function DelPrice()
	{
		$id = $this->params['id'];
		$cakeid = $this->params['cakeid'];
		$obj = $this->GetModel("cake");
		$cnt = $obj->DelPrice($cakeid, $id);
		if($cnt == 0)
			echo "3";
		else
		{
			$cnt = $this->DefaultPrice($cakeid);
			if($cnt == 0)
				echo "2";
			else
				echo "0";
			$this->CoreLog('cake_delprice', $cakeid, $id, '');
		}
		return RT_NONE;
	}

	private function DefaultPrice($id, $setid = 0)
	{
		$obj = $this->GetModel("cake");
		$data = $obj->GetPrices($id);
		$price = 0;
		$unit = '';
		$unprice = false;
		if($data)
		{
			$i = 0;
			do {
				$pid = $data[$i]['id'];
				$price = $data[$i]['price'];
				$unit = $data[$i]['unit'];
				$i++;
			} while($setid != 0 && $pid != $setid);		//如果未指定setid, 则设置了按default排序之后的第一个，否则，设置了setid
			$obj->SetDefaultById($pid, $id);
		}
		else
			$unprice = true;
		return $obj->UpdateVersion2($id, $price, $unit, $unprice);
	}

	private function StoreFile($file, $name)
	{
		if($file['type'] != 'image/jpeg')
			return 1;
		if($file['size'] > 1024 * 1024)
			return 2;
		if($file['error'] > 0)
			return 3;
		$filename = CAKEIMG_DIR . $name;
		if(!move_uploaded_file($file['tmp_name'], $filename))
			return 4;
		return 0;
	}
}

